package io.lckgroup.modules.lck.controller.app;

import io.lckgroup.common.utils.R;
import io.lckgroup.modules.lck.annotation.AppLogin;
import io.lckgroup.modules.lck.annotation.AppLoginUser;
import io.lckgroup.modules.lck.entity.ShopMerchantInfoEntity;
import io.lckgroup.modules.lck.service.OrderMainService;
import io.lckgroup.modules.lck.utils.MyDateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author srq
 * @email siruiqi@siruiqi.com
 * @date 2020-2-17 11:27:38
 */
@RestController
@RequestMapping("/app/count")
@Api("处理统计请求的接口")
public class AppCountController {


    @Autowired
    private OrderMainService orderMainService;

    /**
     * 获取商家固定时间段的统计数据
     *
     * @param sm
     * @return R
     */
    @AppLogin
    @GetMapping("getStaticCount")
    @ApiOperation("获取商家固定时间段的统计数据")
    public R getStaticCount(@AppLoginUser ShopMerchantInfoEntity sm) {
        String today = MyDateUtils.getToday();
        String todayStart = today + " 00:00:00";
        String todayEnd = today + " 23:59:59";
        String weekStart = MyDateUtils.getWeekStart();
        String monthStart = MyDateUtils.getMonthStart();
        ShopMerchantInfoEntity merchant = sm;
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("todayStart", todayStart);
        params.put("todayEnd", todayEnd);
        params.put("weekStart", weekStart);
        params.put("monthStart", monthStart);
        params.put("merchantId", merchant.getId());
        Map<String, Object> result = orderMainService.getStaticCountData(params);

        return R.ok().put("results", result);

    }

    /**
     * 获取商家指定时间段的统计数据
     *
     * @param params
     * @param sm
     * @return R
     */
    @AppLogin
    @GetMapping("getDynamicCount")
    @ApiOperation("获取商家指定时间段的统计数据")
    public R getDynamicCount(@RequestParam Map<String, Object> params, @AppLoginUser ShopMerchantInfoEntity sm) {
        ShopMerchantInfoEntity merchant = sm;
        Map<String, Object> p = new HashMap<String, Object>();
        p.put("startTime", params.get("startTime"));
        p.put("endTime", params.get("endTime"));
        p.put("merchantId", merchant.getId());
        Map<String, Object> result = orderMainService.getDynamicCountData(p);
        return R.ok().put("results", result);
    }

}
